package commands.gameCommand
{
	import Iterator.IIterator;
	
	import commands.ICommand;
	
	public class GamePlayerCommand implements ICommand
	{
		protected var _piece:PieceData;
		public function GamePlayerCommand(piece:PieceData)
		{
			this._piece = piece;
		}
		
		public function execute():void
		{
			var gameboard:GameboardData = GameboardData.getInstance();
			var newGamePiece:PieceData = gameboard.newGamePiece;
			var currentGamePlayer:GamePlayer = newGamePiece.owner;
			
			if(_piece.owner is NullOwner){
				_piece.owner = currentGamePlayer;
				_piece.count = newGamePiece.count;
				
				var iterator:IIterator = gameboard.getProximityPieces(_piece);
				var piece:PieceData;
				while(iterator.hasNext()){
					piece = iterator.next() as PieceData;
					if(piece.owner == _piece.owner){
						piece.count++;
					}else if(!(piece.owner is NullOwner)){
						if(piece.count < _piece.count){
							piece.owner = currentGamePlayer;
						}
					}
				}
				GameboardData.getInstance().nextGamePiece();
			}
		}
	}
}